<template>
  <div class="hotel-warp">
    <a-row>
      <a-col :span="5">
        <div class="hotel-tree-warp">
          <span>宿舍架构：</span>
          <a-tree
            :class="['scrollbar']"
            style="overflow-y: auto; height: calc(100% - 20px)"
            :blockNode="true"
            :checkable="true"
            :data="state.treeData"
            @check="onTreeSelect"
            check-strictly
          >
          </a-tree>
        </div>
      </a-col>
      <a-col :span="19">
        <div class="hotel-table-warp">
          <div class="table-search">
            <a-form :model="state.tableForm" @submit="handleSubmit">
              <a-row>
                <a-col :span="8">
                  <a-form-item field="dateRanges" label="扣分时间段">
                    <a-range-picker
                      style="width: 100%"
                      :value-format="'YYYY-MM-DD'"
                      v-model="state.tableForm.dateRanges"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="13"> </a-col>
                <a-col :span="3">
                  <a-form-item style="justify-content: flex-end">
                    <a-button @click="handleReset">重置</a-button>
                    <a-button
                      @click="handleSearch"
                      style="margin-left: 5px"
                      type="primary"
                    >
                      查询
                    </a-button>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <!-- <div class="table-actions">
            <a-button type="primary">导出</a-button>
          </div> -->
          <a-table
            :scroll="{ y: 660 }"
            row-key="code"
            :columns="columns"
            :data="state.tableData"
            :row-selection="rowSelection"
            v-model:selectedKeys="selectedKeys"
            :pagination="false"
          >
            <template #optional="{ record }">
              <a-button @click="handleUpdate(record)">编辑</a-button>
            </template>
          </a-table>
          <div class="pagetion">
            <a-pagination
              :total="state.tableTotal"
              :page-size="state.pageInfo.pageSize"
              @change="pageChange"
              @page-size-change="pageSzChange"
              show-page-size
            />
          </div>
        </div>
      </a-col>
    </a-row>

    <a-modal
      v-model:visible="state.treeModelVisible"
      @ok="handleModel"
      :unmount-on-close="true"
      @cancel="() => (state.treeModelVisible = false)"
    >
      <template #title>
        {{ state.treeModelType === "add" ? "新增" : "编辑" }}
      </template>
      <a-form :model="state.treeForm" auto-label-width>
        <a-form-item field="name" label="宿舍名称">
          <a-input v-model="state.treeForm.name" />
        </a-form-item>
      </a-form>
    </a-modal>

    <EditForm ref="editFormRef" @refresh="getTableData()"></EditForm>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { formatTree } from "@/utils/tree";
import EditForm from "./editForm.vue";
import Modal from "@arco-design/web-vue/es/modal";
import {
  useEmployeeDeleteApi,
  useEmployeePageApi,
  useHotelCreateApi,
  useHotelDeleteApi,
  useHotelDetailApi,
  useHotelListApi,
  useHotelSumApi,
  useHotelUpdateApi,
  usePositionListApi,
} from "@/api";
import { Message } from "@arco-design/web-vue";
const editFormRef = ref();

// 树形Form
const useTreeForm = () => {
  return {
    name: "",
  };
};
// 表格Form
const useTableForm = () => {
  return {
    startDate: "",
    endDate: "",
    dateRanges: [],
  };
};
const state = reactive({
  tableForm: useTableForm(),
  treeForm: useTreeForm(),
  tableTotal: 0,
  tableData: [],
  pageInfo: {
    pageNum: 1,
    pageSize: 10,
  },
  hotelOrgCode: [],
  currentTreeNode: null, //当前树形选中节点
  treeModelVisible: false, //控制树形弹窗
  treeData: [], //树形数据
  treeSearch: "", //控制树形弹窗
  treeModelType: "add", //树形弹窗类型
  modelVisible: false, //控制表单弹窗
  deleteModelVisible: false, //控制删除弹窗
  positionList: [], //职位数据
});

onMounted(() => {
  getTableData();
  getTreeData();
});

const pageChange = (c) => {
  state.pageInfo.pageNum = c;
  getTableData();
};
const pageSzChange = (p) => {
  state.pageInfo.pageSize = p;
  getTableData();
};

const getTreeData = () => {
  useHotelListApi({}).then(({ data }) => {
    data = data?.map((item) => {
      return {
        ...item,
        key: item.code,
        title: item.name,
      };
    });
    let tree = formatTree(data, "");
    state.treeData = tree;
  });
};

//树形结构搜索
const onTreeSearch = () => {
  console.log("onTreeSearch", state.treeSearch);
};
//树形结构搜索
const onTreeSearchClear = () => {
  console.log("onTreeSearch", state.treeSearch);
};

// 新增宿舍
const onAddDep = (nodeData: any) => {
  state.treeModelType = "add";
  state.treeModelVisible = true;
  state.currentTreeNode = nodeData;
};
// 编辑宿舍
const onEditDep = (nodeData: any) => {
  state.currentTreeNode = nodeData;
  state.treeModelType = "edit";
  state.treeModelVisible = true;
  state.treeForm.name = nodeData.title;
};

const handleModel = () => {
  if (state.treeModelType === "add") {
    if (state.currentTreeNode !== "") {
      useHotelCreateApi(
        Object.assign({}, state.treeForm, {
          parentCode: state.currentTreeNode.code,
        })
      ).then(() => {
        getTreeData();
        state.treeForm = useTreeForm();
      });
      return;
    }
    useHotelCreateApi(
      Object.assign({}, state.treeForm, { parentCode: "" })
    ).then(() => {
      getTreeData();
      state.treeForm = useTreeForm();
    });
  } else {
    useHotelUpdateApi(
      Object.assign({}, state.treeForm, { code: state.currentTreeNode.code })
    ).then(() => {
      getTreeData();
      state.treeForm = useTreeForm();
    });
  }
};
// 删除宿舍
const onDelDep = (nodeData: any) => {
  state.currentTreeNode = nodeData;
};
// 确认删除宿舍
const delDep = () => {
  useHotelDeleteApi({ codeList: [state.currentTreeNode.code] }).then(() => {
    getTreeData();
  });
};

// 表格搜索重置
const handleReset = () => {
  state.tableForm = useTableForm();
  state.pageInfo.pageNum = 1;
  state.pageInfo.pageSize = 10;
  getTableData();
};
// 表格搜索查询
const handleSearch = () => {
  state.tableForm.startDate =
    state.tableForm.dateRanges && state.tableForm.dateRanges[0]
      ? state.tableForm.dateRanges[0]
      : "";
  state.tableForm.endDate =
    state.tableForm.dateRanges && state.tableForm.dateRanges[1]
      ? state.tableForm.dateRanges[1]
      : "";
  getTableData();
};

//获取表格数据
const getTableData = () => {
  useHotelDetailApi(
    Object.assign({}, state.pageInfo, state.tableForm, {
      hotelOrgCodeList: state.hotelOrgCode,
    })
  ).then(({ data: { list, total } }) => {
    state.tableTotal = total;
    state.tableData = list;
  });
};

const onTreeSelect = (selectedKeys, data) => {
  // console.log(selectedKeys, data, "selectedKeys,data");
  state.hotelOrgCode = selectedKeys;
  getTableData();
};

const handleCreate = () => {
  editFormRef.value.open("create");
};
const handleUpdate = (row: any) => {
  editFormRef.value.open("update", row);
};
const handleDelete = () => {
  console.log(selectedKeys.value);
  Modal.warning({
    title: "删除",
    content: "是否确认删除选中的数据？",
    modalStyle: { textAlign: "center", fontSize: "16px" },
    bodyStyle: { fontSize: "14px" },
    cancelText: "取消",
    width: 300,
    closable: true,
    onOk: () => {
      useEmployeeDeleteApi({ codeList: selectedKeys.value })
        .then(() => {
          Message.success("操作成功");
        })
        .catch(() => Message.error("操作失败"))
        .finally(() => {
          selectedKeys.value = [];
          getTableData();
        });
    },
  });
};

const handleSubmit = () => {
  console.log("11");
};

const rowSelection = reactive({
  type: "checkbox",
  showCheckedAll: false,
  onlyCurrent: false,
});

const selectedKeys = ref([]);
const columns = [
  {
    title: "房间号",
    dataIndex: "hotelOrgName",
    align: "center",
  },
  {
    title: "扣分分值",
    dataIndex: "score",
    align: "center",
  },
  {
    title: "违规时间",
    dataIndex: "eventDay",
    align: "center",
  },
];
</script>

<style scoped lang="less">
.hotel-warp {
  .hotel-tree-warp {
    border-right: 1px solid #e8e8e8;
    height: calc(100vh - 110px);
    overflow-y: hidden;
    padding: 10px;

    .tree-search {
      height: 52px;
    }
    .top-actions {
      display: flex;
      justify-content: flex-end;
      cursor: pointer;
    }
  }
  .hotel-table-warp {
    padding: 10px;

    .table-search {
      :deep(.arco-form-item) {
        margin-bottom: 0 !important;
      }
      :deep(.arco-form-item-content-flex) {
        justify-content: flex-end !important;
      }
      margin-bottom: 5px;
    }
    .table-actions {
      display: flex;
      justify-content: flex-end;
      margin-bottom: 5px;
    }
  }
  .pagetion {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
